const express = require("express");
const router = express.Router();
const { v1: uuidV1 } = require("uuid");
var bodyParser = require("body-parser");

// 创建application/json 解析器
var jsonParser = bodyParser.json();

let connectDAO = require("../connect.js");
async function dbTrainInfo(sql, processData) {
  //processData为回调方法
  await connectDAO.queryDataBySQL(sql, processData);
}

router.get("/resources_list", function (res, req) {
  dbTrainInfo(`SELECT * FROM resources;`, (result) => {
    req.send({ code: 200, msg: "成功", result });
  });
});
router.delete("/del_resources", function (res, req) {
  const { id } = res.body;
  dbTrainInfo(`DELETE FROM resources WHERE id=${id};`, () => {
    req.send({ code: 200, msg: "成功" });
  });
});
router.post("/update_resources", function (res, req) {
  const { id, url } = res.body;
  dbTrainInfo(`UPDATE resources SET url='${url}' WHERE id = '${id}';`).then(
    () => {
      req.send({ code: 200, msg: "成功" });
    }
  );
});
router.post("/add_resources", function (res, req) {
  let { position, url } = res.body;
  const obj = {
    '导航图标': 'headNav',
    '首页banner': 'homeBanner',
    '模块banner': 'moduleBanner',
    '友链bg': 'friendLink'
  }
  position = obj[position]
  dbTrainInfo(
    `INSERT INTO resources VALUES ( '${uuidV1()}', '${url}', '${position}', '' );`
  ).then(() => {
    req.send({ code: 200, msg: "添加成功" });
  });
});
module.exports = router;
